﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.Business.DAL
{
    /// <summary>
    /// 数据访问类t_Contract_PointAllotInfo。
    /// </summary>
    public class t_Contract_PointAllotInfo
    {
        public t_Contract_PointAllotInfo()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid RecordId)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_Contract_PointAllotInfo where RecordId=@RecordId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.Business.Entity.t_Contract_PointAllotInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_Contract_PointAllotInfo(");
            strSql.Append("RecordId,ContractId,BuyerOrVenderInfo,Point,MajordomoName,MajordomoId,RegionManagerName,RegionManagerId,MangerName,MangerId,DeparterManagerId,DeparterManagerName,Employee,EmployeeId,Depart,DepartId,CreateTime,UpdateTime)");

            strSql.Append(" values (");
            strSql.Append("@RecordId,@ContractId,@BuyerOrVenderInfo,@Point,@MajordomoName,@MajordomoId,@RegionManagerName,@RegionManagerId,@MangerName,@MangerId,@DeparterManagerId,@DeparterManagerName,@Employee,@EmployeeId,@Depart,@DepartId,@CreateTime,@UpdateTime)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "ContractId", DbType.Guid, model.ContractId);
            db.AddInParameter(dbCommand, "BuyerOrVenderInfo", DbType.AnsiString, model.BuyerOrVenderInfo);
            db.AddInParameter(dbCommand, "Point", DbType.Decimal, model.Point);
            db.AddInParameter(dbCommand, "MajordomoName", DbType.AnsiString, model.MajordomoName);
            db.AddInParameter(dbCommand, "MajordomoId", DbType.Guid, model.MajordomoId);
            db.AddInParameter(dbCommand, "RegionManagerName", DbType.AnsiString, model.RegionManagerName);
            db.AddInParameter(dbCommand, "RegionManagerId", DbType.Guid, model.RegionManagerId);
            db.AddInParameter(dbCommand, "MangerName", DbType.AnsiString, model.MangerName);
            db.AddInParameter(dbCommand, "MangerId", DbType.Guid, model.MangerId);
            db.AddInParameter(dbCommand, "DeparterManagerId", DbType.Guid, model.DeparterManagerId);
            db.AddInParameter(dbCommand, "DeparterManagerName", DbType.AnsiString, model.DeparterManagerName);
            db.AddInParameter(dbCommand, "Employee", DbType.AnsiString, model.Employee);
            db.AddInParameter(dbCommand, "EmployeeId", DbType.Guid, model.EmployeeId);
            db.AddInParameter(dbCommand, "Depart", DbType.AnsiString, model.Depart);
            db.AddInParameter(dbCommand, "DepartId", DbType.Guid, model.DepartId);
            db.AddInParameter(dbCommand, "CreateTime", DbType.DateTime, model.CreateTime);
            db.AddInParameter(dbCommand, "UpdateTime", DbType.DateTime, model.UpdateTime);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.Business.Entity.t_Contract_PointAllotInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_Contract_PointAllotInfo set ");
            strSql.Append("ContractId=@ContractId,");
            strSql.Append("BuyerOrVenderInfo=@BuyerOrVenderInfo,");
            strSql.Append("Point=@Point,");
            strSql.Append("MajordomoName=@MajordomoName,");
            strSql.Append("MajordomoId=@MajordomoId,");
            strSql.Append("RegionManagerName=@RegionManagerName,");
            strSql.Append("RegionManagerId=@RegionManagerId,");
            strSql.Append("MangerName=@MangerName,");
            strSql.Append("MangerId=@MangerId,");
            strSql.Append("DeparterManagerId=@DeparterManagerId,");
            strSql.Append("DeparterManagerName=@DeparterManagerName,");
            strSql.Append("Employee=@Employee,");
            strSql.Append("EmployeeId=@EmployeeId,");
            strSql.Append("Depart=@Depart,");
            strSql.Append("DepartId=@DepartId,");
            strSql.Append("CreateTime=@CreateTime,");
            strSql.Append("UpdateTime=@UpdateTime");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "ContractId", DbType.Guid, model.ContractId);
            db.AddInParameter(dbCommand, "BuyerOrVenderInfo", DbType.AnsiString, model.BuyerOrVenderInfo);
            db.AddInParameter(dbCommand, "Point", DbType.Decimal, model.Point);
            db.AddInParameter(dbCommand, "MajordomoName", DbType.AnsiString, model.MajordomoName);
            db.AddInParameter(dbCommand, "MajordomoId", DbType.Guid, model.MajordomoId);
            db.AddInParameter(dbCommand, "RegionManagerName", DbType.AnsiString, model.RegionManagerName);
            db.AddInParameter(dbCommand, "RegionManagerId", DbType.Guid, model.RegionManagerId);
            db.AddInParameter(dbCommand, "MangerName", DbType.AnsiString, model.MangerName);
            db.AddInParameter(dbCommand, "MangerId", DbType.Guid, model.MangerId);
            db.AddInParameter(dbCommand, "DeparterManagerId", DbType.Guid, model.DeparterManagerId);
            db.AddInParameter(dbCommand, "DeparterManagerName", DbType.AnsiString, model.DeparterManagerName);
            db.AddInParameter(dbCommand, "Employee", DbType.AnsiString, model.Employee);
            db.AddInParameter(dbCommand, "EmployeeId", DbType.Guid, model.EmployeeId);
            db.AddInParameter(dbCommand, "Depart", DbType.AnsiString, model.Depart);
            db.AddInParameter(dbCommand, "DepartId", DbType.Guid, model.DepartId);
            db.AddInParameter(dbCommand, "CreateTime", DbType.DateTime, model.CreateTime);
            db.AddInParameter(dbCommand, "UpdateTime", DbType.DateTime, model.UpdateTime);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_Contract_PointAllotInfo ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            db.ExecuteNonQuery(dbCommand);

        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void DeletePoint(Guid RecordID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_Contract_PointAllotInfo ");
            strSql.Append(" where ContractId=@RecordID ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordID", DbType.Guid, RecordID);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Business.Entity.t_Contract_PointAllotInfo GetModel(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,ContractId,BuyerOrVenderInfo,Point,MajordomoName,MajordomoId,RegionManagerName,RegionManagerId,MangerName,MangerId,DeparterManagerId,DeparterManagerName,Employee,EmployeeId,Depart,DepartId,CreateTime,UpdateTime from t_Contract_PointAllotInfo ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            Chenxin.Lib.Business.Entity.t_Contract_PointAllotInfo model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,ContractId,BuyerOrVenderInfo,Point,MajordomoName,MajordomoId,RegionManagerName,RegionManagerId,MangerName,MangerId,DeparterManagerId,DeparterManagerName,Employee,EmployeeId,Depart,DepartId,CreateTime,UpdateTime ");
            strSql.Append(" FROM t_Contract_PointAllotInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }


        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageIndex, int PageSize, String OrderField, string TableName, string StrWhere, string FieldList, string TableFieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, TableName);
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, StrWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.Business.Entity.t_Contract_PointAllotInfo> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,ContractId,BuyerOrVenderInfo,Point,MajordomoName,MajordomoId,RegionManagerName,RegionManagerId,MangerName,MangerId,DeparterManagerId,DeparterManagerName,Employee,EmployeeId,Depart,DepartId,CreateTime,UpdateTime ");
            strSql.Append(" FROM t_Contract_PointAllotInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.Business.Entity.t_Contract_PointAllotInfo> list = new List<Chenxin.Lib.Business.Entity.t_Contract_PointAllotInfo>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.Business.Entity.t_Contract_PointAllotInfo ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.Business.Entity.t_Contract_PointAllotInfo model = new Chenxin.Lib.Business.Entity.t_Contract_PointAllotInfo();
            object ojb;
            ojb = dataReader["RecordId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RecordId = new Guid(ojb.ToString());
            }
            ojb = dataReader["ContractId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ContractId = new Guid(ojb.ToString());
            }
            model.BuyerOrVenderInfo = dataReader["BuyerOrVenderInfo"].ToString();
            ojb = dataReader["Point"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Point = (decimal)ojb;
            }
            model.MajordomoName = dataReader["MajordomoName"].ToString();
            ojb = dataReader["MajordomoId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.MajordomoId = new Guid(ojb.ToString());
            }
            model.RegionManagerName = dataReader["RegionManagerName"].ToString();
            ojb = dataReader["RegionManagerId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RegionManagerId = new Guid(ojb.ToString());
            }
            model.MangerName = dataReader["MangerName"].ToString();
            ojb = dataReader["MangerId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.MangerId = new Guid(ojb.ToString());
            }
            ojb = dataReader["DeparterManagerId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DeparterManagerId = new Guid(ojb.ToString());
            }
            model.DeparterManagerName = dataReader["DeparterManagerName"].ToString();
            model.Employee = dataReader["Employee"].ToString();
            ojb = dataReader["EmployeeId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.EmployeeId = new Guid(ojb.ToString());
            }
            model.Depart = dataReader["Depart"].ToString();
            ojb = dataReader["DepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DepartId = new Guid(ojb.ToString());
            }
            ojb = dataReader["CreateTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CreateTime = (DateTime)ojb;
            }
            ojb = dataReader["UpdateTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.UpdateTime = (DateTime)ojb;
            }
            return model;
        }

        #endregion  成员方法
    }
}

